############# Set up #############

library(pacman)

p_load(
  tidyverse, data.table, lubridate, rvest, purrr, foreign, readstata13,
  ggplot2, janitor, ranger, parsnip, haven, sf, rebus, gridExtra,
  magrittr, here, dplyr, date, conflicted, mefa, stringr, geosphere
)

# Set Directory
# ! change the directory 
setwd("")
wd <- getwd()

# Start the clock----------
ptm <- proc.time()

# Set year range
fromyr <- 2004
thruyr <- 2016

# Set pollutant parameter
# Ozone(44201) SO2(42401) CO(42101) NO2(42602) PM2.5(88101) PM10(81102)\

pollutants <- c(42101, 42401, 42602, 44201, 81102, 88101)

##### download and save data #####

for (i in 1:length(pollutants)){
  poll <- pollutants[i]
  source("scripts/R script/1_1. download_save_date.R")
}

########## prepare data ##########

## This step is not needed as well as interim data are all saved
for (i in 1:length(pollutants)){
  poll <- pollutants[i]
  source("scripts/R script/1_2. create_annual_concentration.R")
  source("scripts/R script/1_3. create_daily_monitor.R")
  source("scripts/R script/1_4. proc_act_day.R")
}


# This is the main code generating the coefficients tables

####### build event study ########


for (i in 1:length(pollutants)){
  poll <- pollutants[i]
  source("scripts/R script/2. build_evnt_study.R")
}

# Stop the clock------------
proc.time() - ptm

